package cn.cd.anStudio.util;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MyBusPropertySQLHelper extends SQLiteOpenHelper {
	
	//DB general information
	private final static int DATABASE_VERSION = 1;
	
	private final static String MYBUS_PROPERTY_DB_NAME               = "myBusPro_db";
    private final static String MYBUS_PROPERTY_DB_TABLE_NAME         = "myBusPro";
    private final static String MYBUS_PROPERTY_DB_ID_KEY             = "_id";
    public  final static String MYBUS_PROPERTY_DB_PROPERTY_NAME_KEY  = "_property";
    public  final static String MYBUS_PROPERTY_DB_PROPERTY_VALUE_KEY = "_value";

    //DB internal architecture
    public final static String MYBUS_PROPERTY_DB_VERSION_NAME   = "version";
    
    public MyBusPropertySQLHelper(Context context)
    {
        super(context, MYBUS_PROPERTY_DB_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = 
        		  "Create table "
                + MYBUS_PROPERTY_DB_TABLE_NAME
                + "(" + MYBUS_PROPERTY_DB_ID_KEY
                + " integer primary key autoincrement,"
                + MYBUS_PROPERTY_DB_PROPERTY_NAME_KEY 
                + " text not null,"
                + MYBUS_PROPERTY_DB_PROPERTY_VALUE_KEY
                + " text);";
        db.execSQL(sql);
    }

	@Override
	public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
		String sql = " DROP TABLE IF EXISTS " + MYBUS_PROPERTY_DB_TABLE_NAME;
        db.execSQL(sql);
        onCreate(db);
	}
	
	public Cursor select()
    {
        SQLiteDatabase db=this.getReadableDatabase();
        Cursor cursor=db.query(MYBUS_PROPERTY_DB_TABLE_NAME, null, null, null, null, null,  " _id desc");
        return cursor;
    }
    
    public long insert(String property, String value)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        
        ContentValues cv = new ContentValues();
        cv.put(MYBUS_PROPERTY_DB_PROPERTY_NAME_KEY, property);
        cv.put(MYBUS_PROPERTY_DB_PROPERTY_VALUE_KEY, value);
        
        long row = db.insert(MYBUS_PROPERTY_DB_TABLE_NAME, null, cv);
        
        return row;
    }
    
    public void delete(int id)
    {
        SQLiteDatabase db = this.getWritableDatabase();

        String[] whereValue = {Integer.toString(id)};
        db.delete(MYBUS_PROPERTY_DB_TABLE_NAME, MYBUS_PROPERTY_DB_ID_KEY + "=?", whereValue);
    }

    public void update(int id, String property, String value)
    {
        SQLiteDatabase db = this.getWritableDatabase();

        String[] whereValue = {Integer.toString(id)};
        
        ContentValues cv = new ContentValues(); 
        cv.put(MYBUS_PROPERTY_DB_PROPERTY_NAME_KEY, property);
        cv.put(MYBUS_PROPERTY_DB_PROPERTY_VALUE_KEY, value);

        db.update(MYBUS_PROPERTY_DB_TABLE_NAME, cv, MYBUS_PROPERTY_DB_ID_KEY+"=?", whereValue);
    }
}
